//
//  MutableWeakArray.m
//  MyProject
//
//  Created by danal on 13-5-8.
//  Copyright (c) 2013年 danal. All rights reserved.
//

#import "MutableWeakArray.h"

@implementation MutableWeakArray

- (void)dealloc{
    vec.clear();
    [super dealloc];
}

- (id)init{
    self = [super init];
    if (self){
    }
    return self;
}

- (int)count{
    return vec.size();
}

- (void)addObject:(id)object{
    vec.push_back(object);
}

- (void)addObjectsFromArray:(NSArray *)otherArray{
    for (id object  in otherArray) {
        vec.push_back(object);
    }
}

- (id)objectAtIndex:(int)index{
    if (index > vec.size()) {
        NSAssert(0, @"%d is over bounds of %ld",index,vec.size());
    }
    std::vector<id>::iterator it = vec.begin();
    it += index;
    return vec.at(index);
    //return vec[index];
}

- (void)removeObjectAtIndex:(int)index{
    std::vector<id>::iterator it = vec.begin();
    it += index;
    vec.erase(it);
}

- (void)removeLastObject{
    vec.pop_back();
}

- (void)removeAllObjects{
    vec.clear();
}

- (void)replaceObjectAtIndex:(int)index withObject:(id)object{
    std::vector<id>::iterator it = vec.begin();
    it += index;
    *it = object;
}

- (void)insertObject:(id)object atIndex:(NSUInteger)index{
    std::vector<id>::iterator it = vec.begin();
    it += index;
    vec.insert(it, object);
}

- (void)removeObjectsInRange:(NSRange)range{
    std::vector<id>::iterator it = vec.begin();
    for (int i = range.location; i < range.location + range.length; i++) {
        vec.erase(it+i);
    }
}

@end
